from ctypes import *

# msg ID 

## from device
Login_Request = 0x01
Heart_Request = 0x03
Price_Check_Request = 0x05
Price_Request = 0x09
Monitor_Upload = 0x13
BMS_Shake_Upload = 0x15
BMS_Config_Upload = 0x17
BMS_End_Upload = 0x19
BMS_Error_Upload = 0x1B
BMS_BST_Upload = 0x1D
BMS_CST_Upload = 0x21
BMS_BCL_Upload = 0x23
BMS_BCS_Upload = 0x25
Device_Start_Request = 0x31
Platform_Start_Respond = 0x33
Platform_Stop_Respond = 0x35
Bill_Info_Upload = 0x3B
Time_Confirm = 0x55
Price_Confirm = 0x57
QR_Confirm = 0x59
Reboot_Confirm = 0x91

## from platform
Login_Respond = 0x02
Heart_Respond = 0x04
Price_Check_Respond = 0x06
Price_Respond = 0x0A
Monitor_Request = 0x12
Device_Start_Respond = 0x32
Platform_Start_Request = 0x34
Platform_Stop_Request = 0x36
Bill_Info_Confirm = 0x40
Time_Set = 0x56
Price_Set = 0x58
QR_Set = 0x5A
Reboot_Set = 0x92

class pack_head(Structure):
    _pack_ = 1
    _fields_ = [
        ("indentifer", c_uint8),
        ("pack_size", c_uint8),
        ("pack_index", c_uint16),
        ("encode_type", c_uint8),
        ("msg_ID", c_uint8),
    ]

class Bill_Info_Upload_Struct(Structure):
    _pack_ = 1
    _fields_ = [
        ("Serial_Num", c_uint8 * 16),
        ("Device_ID", c_uint8 * 10),
        ("gun_num", c_uint8),
        ("Charge_Start_Time", c_uint8 * 7),
        ("Charge_End_Time", c_uint8 * 7),

        ("tip_price", c_int),
        ("tip_energy", c_int),
        ("tip_energy_loss", c_int),
        ("tip_fee", c_int),

        ("peak_price", c_int),
        ("peak_energy", c_int),
        ("peak_energy_loss", c_int),
        ("peak_fee", c_int),

        ("flat_price", c_int),
        ("flat_energy", c_int),
        ("flat_energy_loss", c_int),
        ("flat_fee", c_int),

        ("valley_price", c_int),
        ("valley_energy", c_int),
        ("valley_energy_loss", c_int),
        ("valley_fee", c_int),

        ("Charge_Start_ElcMeter", c_uint8 * 5),
        ("Charge_End_ElcMeter", c_uint8 * 5),
        ("Charge_Energy", c_int),
        ("Charge_Energy_loss", c_int),
        ("Pay_Money", c_int),
        ("VIN", c_uint8 * 17),
        ("bill_identifer", c_uint8),
        ("Bill_time", c_uint8 * 7),
        ("Charge_Stop_Reason", c_uint8),
        ("Card_ID_ex", c_uint8 * 8),
    ]

class Monitor_Upload_Struct(Structure):
    _pack_ = 1
    _fields_ = [
        ("Serial_Num", c_uint8 * 16),
        ("Device_ID", c_uint8 * 10),
        ("gun_num", c_uint8),
        
        ("Gun_State", c_uint8),
        ("Gun_Physical_State", c_uint8),
        ("Gun_insert_State", c_uint8),

        ("output_U", c_uint16),
        ("output_I", c_uint16),
        ("gun_temp", c_uint8),
        ("gun_code", c_uint8 * 8),
        ("SOC", c_uint8),
        ("batterys_highest_temp", c_uint8),
        ("charge_time", c_uint16),
        ("remain_charge_time", c_uint16),

        ("charged_energy", c_int),
        ("loss_energy", c_int),
        ("pay_money", c_int),
        ("physical_error", c_uint16),
    ]

def input_data():
    user_input = input("请输入你的报文：")
    hex_groups = [user_input[i:i+2] for i in range(0, len(user_input), 2)]
    return hex_groups

msg_head_len = 6
def pack_analyze(single_pack):
    # 数据切片
    head_data = single_pack[:msg_head_len]
    msg_data = single_pack[msg_head_len:-2]
    crc = single_pack[-2:]
    print(head_data)

    test_data = [20,10,0,0,10,20]
    print(test_data)
    my_struct = pack_head()
    my_struct.indentifer = test_data[0]
    head_data = pack_head.from_buffer_copy(test_data)  # 将列表转换为字节

    # 帧头解析
    msg_head = pack_head.from_buffer_copy(head_data)

    print("报文长度：", msg_head.pack_size)
    print("报文ID:", msg_head.msg_ID)
    print("报文 crc:", crc)
    # match msg_head.msg_ID:
    #     case Bill_Info_Upload:

    #     return "Case 1"

def main_loop():
    while True:  # 主循环
        hex_groups = input_data()
        pack_analyze(hex_groups)

if __name__ == "__main__":
    print("ykc_V1.6 报文解析")
    main_loop()